/*
 * 
 */
package com.gidas.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import java.nio.charset.CharsetEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.gidas.server.MyXMLParser;

// TODO: Auto-generated Javadoc
/**
 * The Class xmlUploadFormHandler.
 */
@SuppressWarnings("serial")
public class xmlUploadFormHandler extends HttpServlet {
	
	/** The content. */
	String content = null;
	
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException 
	{
		try 
		{
			ServletFileUpload upload = new ServletFileUpload();
			upload.setSizeMax(50000000);
			//res.setCharacterEncoding("iso-8859-13");
			//res.setCharacterEncoding("windows-1257");
			res.setCharacterEncoding("utf-8");
			
			res.setContentType("text/plain");
			PrintWriter out = res.getWriter();
			//PrintWriter out = res.ServletOutputStream() ;
			try 
			{
				FileItemIterator iterator = upload.getItemIterator(req);
				while (iterator.hasNext()) 
				{
					FileItemStream item = iterator.next();
					InputStream in = item.openStream();
				
					
					String fieldName = item.getFieldName();
					String fileName = item.getName();
					
					String contentType = item.getContentType();
					out.println("contentType = " + contentType);
					//check for xml type
					
					
					//FieldName is Date and City. Format yyyy.mm.d.city
					String yearString = fieldName.substring(0,4);
					String monthString = fieldName.substring(5,7);
					String dayString = fieldName.substring(8,10);
					String dateString = ""+yearString+"/"+monthString+"/"+dayString;
					String city = fieldName.substring(11);
					
					out.println(yearString+"/"+monthString+"/"+dayString+"+"+city);
					
					
					//http://www.kodejava.org/examples/19.html - How do I convert String to Date object?
					DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
					Date stopValidDate = new Date();
					try
			        {
			            stopValidDate = df.parse(dateString);            
			            
			        } catch (ParseException e)
			        {
			            e.printStackTrace();
			        }
			        

					
					//out.println("--------------");
					out.println("fileName = " + fileName);
					out.println("field name = " + fieldName);
					//out.println("contentType = " + contentType);
					
					
					String fileContents = null;
					try 
					{
						 fileContents = IOUtils.toString(in,"windows-1257");
			
					} 
					finally 
					{
						IOUtils.closeQuietly(in);
						//System.out.println(fileContents);
					
						MyXMLParser myParser = new MyXMLParser();
						//myParser.setCity(city);
						//myParser.city = city;
						//myParser.setDate(stopValidDate);
						
						myParser.parse(fileContents,city,stopValidDate);
						
					}
					out.println("Done upload");
				
					
				}
			} catch (SizeLimitExceededException e) {
			out.println("You exceeded the maximu size ("
			+ e.getPermittedSize() + ") of the file ("
			+ e.getActualSize() + ")");
			}
		} catch (Exception ex) {
	
		throw new ServletException(ex);
		}
		
	}
	

	
	
	
}
